package com.neasho.neashoadminv1.web.console.sys

import com.neasho.neashoadminv1.infrastructure.domain.R
import com.neasho.neashoadminv1.infrastructure.dto.sys.*
import com.neasho.neashoadminv1.infrastructure.permission.ApiAuth
import com.neasho.neashoadminv1.web.service.sys.SysRoleService
import com.neasho.neashoadminv1.infrastructure.utils.UserContext
import jakarta.validation.Valid
import org.babyfish.jimmer.spring.model.SortUtils
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.web.bind.annotation.*

/**
 * 系统 - 角色维护
 *
 */
@RestController
@RequestMapping("/console/sys/role")
class SysRoleController(private val sysRoleService: SysRoleService) {
    val log: org.slf4j.Logger = org.slf4j.LoggerFactory.getLogger(SysRoleController::class.java)
    /**
     * 新增角色
     */
    @PostMapping("/save")
    @ApiAuth([ "sys:role:save" ])
    fun save(@Valid @RequestBody input: SysRoleAddInput): R<Int> {
        return R.affect(sysRoleService.save(input).totalAffectedRowCount)
    }

    /**
     * 修改角色
     */
    @PutMapping("/update")
    @ApiAuth([ "sys:role:update" ])
    fun update(@Valid @RequestBody input: SysRoleAddInput): R<Int> {
        return R.affect(sysRoleService.update(input).totalAffectedRowCount)
    }

    /**
     * 角色分页查询
     */
    @GetMapping("/page")
    @ApiAuth([ "sys:role:page" ])
    fun page(@Valid spec: SysRolePageSpec): R<Page<SysRolePageView>> {
        return R.ok(
            sysRoleService.page(
                spec, SysRolePageView::class, PageRequest.of(
                    spec.pageNumber - 1, spec.pageSize, SortUtils.toSort("id desc")
                )
            )
        )
    }

    /**
     * 查询列表
     */
    @GetMapping("/list")
    @ApiAuth([ "sys:role:list" ])
    fun list(@Valid spec: SysRoleListSpec): R<List<SysRoleListView>> {
        return R.ok(sysRoleService.list(spec, SysRoleListView::class))
    }

    /**
     * 查询角色详情
     */
    @GetMapping("/detail")
    @ApiAuth([ "sys:role:detail" ])
    fun detail(@Valid spec: SysRoleDetailSpec): R<SysRoleDetailView> {
        log.info("logined user name is: ${UserContext.username()}")
        return R.ok(sysRoleService.findById(spec, SysRoleDetailView::class))
    }

    /**
     * 逻辑删除角色
     */
    @DeleteMapping("/delete")
    @ApiAuth([ "sys:role:delete" ])
    fun delete(@Valid spec: SysRoleLogicDeleteInput): R<Int> {
        return R.affect(sysRoleService.deleteByPhysical(spec.id).totalAffectedRowCount)
    }

    /**
     * 批量删除角色
     */
    @DeleteMapping("/batch-delete-by-ids")
    @ApiAuth([ "sys:role:batch-delete-by-ids" ])
    fun batchDeleteByIds(@Valid spec: SysRoleBatchDeleteInput): R<Int> {
        return R.affect(sysRoleService.batchDeleteByIds(spec.ids).totalAffectedRowCount)
    }

    /**
     * 批量修改
     */
    @PutMapping("/batch-update")
    @ApiAuth([ "sys:role:batch-update" ])
    fun batchUpdate(@Valid @RequestBody list: List<SysRoleUpdateInput>): R<Int> {
        return R.affect(sysRoleService.batchUpdate(list))
    }

    /**
     * 批量新增
     */
    @PostMapping("/batch-save")
    @ApiAuth([ "sys:role:batch-save" ])
    fun batchSave(@Valid @RequestBody list: List<SysRoleAddInput>): R<Int> {
        return R.affect(sysRoleService.batchSave(list))
    }

    /**
     * 导入模板
     */
    @GetMapping("/import-template")
    @ApiAuth([ "sys:role:import-template" ])
    fun importTemplate(): R<String> {
        TODO("not yet implemented")
    }

    /**
     * 导出数据
     */
    @GetMapping("/export-data")
    @ApiAuth([ "sys:role:export-data" ])
    fun export(): R<String> {
        TODO("not yet implemented")
    }

    /**
     * 导入数据
     */
    @PostMapping("/import-data")
    @ApiAuth([ "sys:role:import-data" ])
    fun importData(): R<String> {
        TODO("not yet implemented")
    }

}